import { Space, Tag, Popconfirm, Table, Button, Modal, type TableProps } from 'antd';
import React, { useState } from "react"
import Myform from './myform';
interface DataType {
  key: string;
  name: string;
  role: number;
  desc: string;
  tags: string[];
}

export default function Column(props: TableProps<DataType>) {
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [title, setTitle] = useState('添加')
  const [itemobj, setItemobj] = useState({})
  const showModal = () => {
    setIsModalOpen(true);
  };

  const handleOk = () => {
    setIsModalOpen(false);
  };

  const handleCancel = () => {
    setIsModalOpen(false);
  };
  let [data, setData] = useState<DataType[]>([
    {
      key: '1',
      name: '徐小夕',
      role: 1,
      desc: 'H5-Dooring作者, 掘金签约作者，知乎专栏作家',
      tags: ['前端工程师', 'developer'],
    },
    {
      key: '2',
      name: '张三',
      role: 2,
      desc: '知乎专栏作家',
      tags: ['前端工程师', 'developer', 'Dooring'],
    },
    {
      key: '3',
      name: '李盟',
      role: 2,
      desc: 'Dooring共建者',
      tags: ['后端工程师', 'V6.Dooring', 'Dooring'],
    },
    {
      key: '4',
      name: '王阿明',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', '橙子表单'],
    },
    {
      key: '5',
      name: '张小明',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', '橙子表单'],
    },
    {
      key: '6',
      name: 'Tom',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', 'Next-Admin'],
    },
    {
      key: '7',
      name: 'Json',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', 'Next-Admin'],
    }
  ])
  const columns: TableProps<DataType>['columns'] = [
    {
      title: '用户名',
      dataIndex: 'name',
      key: 'name',
      render: (text) => <a>{text}</a>,
      fixed: 'left',
      width: 100
    },
    {
      title: '角色',
      dataIndex: 'role',
      key: 'role',
      render: role => role === 1 ? '超级管理员' : '开发者'
    },
    {
      title: '简介',
      dataIndex: 'desc',
      key: 'desc',
    },
    {
      title: '标签',
      key: 'tags',
      dataIndex: 'tags',
      render: (_, { tags }) => (
        <>
          {tags.map((tag) => {
            let color = tag.length > 5 ? 'geekblue' : 'green';
            if (tag === 'loser') {
              color = 'volcano';
            }
            return (
              <Tag color={color} key={tag}>
                {tag.toUpperCase()}
              </Tag>
            );
          })}
        </>
      ),
    },
    {
      title: '操作',
      key: 'action',
      render: (_, record) => (
        <Space size="middle">
          <a onClick={() => {
            //编辑操作
            showModal()
            setTitle('编辑')
            setItemobj(record)
          }}>编辑</a>
          <Popconfirm
            title="Delete the task"
            description="Are you sure to delete this task?"
            onConfirm={() => {
              //删除操作
              let newData = data.filter((item => {
                return item.key != record.key
              }))
              setData(newData)
            }}
            okText="Yes"
            cancelText="No"
          >
            <a >删除</a>
          </Popconfirm>
        </Space>
      ),
    },
  ];
  const getUniqueRoles = () => {
    const roles = new Set(data.map(item => item.role));
    return Array.from(roles);
  };
  //添加和编辑
  const handleFormSubmit = (values) => {
    let tags:any = []
    if (values.tags === 1) {
      tags = ['前端工程师', '111'];
    } else if (values.tags === 2) {
      tags = ['后端工程师', '222'];
    } else if (values.tags === 3) {
      tags = ['全栈工程师', '333'];
    }
    if (title == '添加') {
      const newKey = Date.now().toString()
      const newItem = {
        ...values,
        key: newKey,
        role: Number(values.role),
        tags
      }
      setData([...data, newItem])
    } else if (title == '编辑') {
      setData(data.map(item => item.key === itemobj.key ? {...values,tags} : item)
      )
    }
    setIsModalOpen(false)
  }
  return (
    <div>
      <Button type="primary" onClick={() => {
        showModal()
        setTitle('添加')
        setItemobj({})
      }}>
        添加
      </Button>
      <Modal title={title} open={isModalOpen} onOk={handleOk} onCancel={handleCancel} footer={null}>
        <Myform title={title} itemobj={itemobj} handleCancel={handleCancel}
          key={new Date().getTime()} data={getUniqueRoles()} onFinish={handleFormSubmit} />
      </Modal>
      <Table columns={columns} dataSource={data} pagination={{ pageSize: 5 }} />
    </div>
  )
}
